//src/main/java/com/cuit/blog/controller/ReplyController.java
package com.cuit.blog.controller;
import com.cuit.blog.entity.Article;
import com.cuit.blog.entity.Reply;
import com.cuit.blog.service.ReplyService;
import com.cuit.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;import org.springframework.ui.Model;

import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/reply")
public class ReplyController {


        @Autowired
        private ReplyService replyService;

        @RequestMapping("/findAll")
        public String findAll(Model model) {
                List<Reply> replies = replyService.findAll();
                model.addAttribute("replies", replies);
                return "reply/reply";
        }
        @GetMapping("/add")
        public String showAddForm(Model model) {
                Reply reply = new Reply();
                model.addAttribute("reply", reply);
                return "reply/add";
        }

        @PostMapping("/add")
        public String addReply(@ModelAttribute Reply reply) {

                reply.setTime(LocalDateTime.now());
                replyService.save(reply);
                return "redirect:/reply/findAll";
        }
        @GetMapping("/delete/{id}")
        public String showDeleteConfirmation(@PathVariable("id") Integer id, Model model) {
                Reply reply = replyService.findById(id);
                model.addAttribute("reply", reply);
                return "reply/delete";
        }

        @PostMapping("/delete/{id}")
        public String deleteReply(@PathVariable("id") Integer id) {
                replyService.deleteById(id);
                return "redirect:/reply/findAll";
        }
        @GetMapping("/edit/{id}")
        public String showEditForm(@PathVariable("id") Integer id, Model model) {
                Reply reply = replyService.findById(id);

                reply.setTime(LocalDateTime.now());
                model.addAttribute("reply", reply);
                return "reply/edit";
        }

        @PostMapping("/edit/{id}")
        public String editReply(@PathVariable("id") Integer id, @ModelAttribute Reply reply) {

                reply.setTime(LocalDateTime.now());
                reply.setId(id);
                replyService.save(reply);
                return "redirect:/reply/findAll";
        }
        @GetMapping("/search")
        public String showSearchForm(Model model) {
                return "reply/search";
        }

        @PostMapping("/result")
        public String searchResult(@RequestParam Integer replyId, Model model) {
                Reply reply = replyService.findById(replyId);
                model.addAttribute("reply", reply);
                return "reply/result";
        }
}
